package educrypt.gui.components;

import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;

import java.util.List;

import educrypt.gui.util.GuiUtil;
import educrypt.gui.view.AnalysisOptionsView;
import educrypt.gui.view.TextFilterView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

class AnalysisOptionsPanel extends EducryptPanel implements AnalysisOptionsView {

	private static final long serialVersionUID = 1L;

	private JLabel lblAnalysisOptions, lblAlphabet, lblNgramSize;
	private JComboBox cbxAlphabet;
	private JButton btnAnalyze, btnFilter, btnAddAlphabet;
	private JSpinner spinnerNgramSize;

	private TextFilterDialog textFilterDialog;

	public AnalysisOptionsPanel() {
		this.setSize(740, 80);
		this.setPreferredSize(this.getSize());
		setLayout(null);

		cbxAlphabet = new JComboBox();
		cbxAlphabet.setBounds(94, 44, 190, 20);
		add(cbxAlphabet);

		lblAlphabet = new JLabel(Messages.getString("guiAlphabet"));
		lblAlphabet.setBounds(31, 43, 58, 22);
		add(lblAlphabet);

		ImageIcon iconAdd = new ImageIcon(
				EduCryptResources.getImageURL("plus24.png"));

		btnAddAlphabet = new JButton(iconAdd);
		btnAddAlphabet.setEnabled(false);
		btnAddAlphabet.setBounds(290, 42, 24, 24);
		add(btnAddAlphabet);

		btnAnalyze = new JButton(Messages.getString("guiAnalyze"));
		btnAnalyze.setBounds(642, 39, 80, 30);
		btnAnalyze.setEnabled(false);
		add(btnAnalyze);

		lblAnalysisOptions = new JLabel(
				Messages.getString("guiAnalysisOptions"));
		lblAnalysisOptions.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblAnalysisOptions.setBounds(31, 11, 249, 19);
		add(lblAnalysisOptions);

		btnFilter = new JButton(Messages.getString("guiFilter"));
		btnFilter.setEnabled(false);
		btnFilter.setBounds(556, 39, 80, 30);
		add(btnFilter);

		lblNgramSize = new JLabel(Messages.getString("guiNgramSize"));
		lblNgramSize.setHorizontalAlignment(SwingConstants.CENTER);
		lblNgramSize.setBounds(340, 44, 136, 21);
		add(lblNgramSize);

		spinnerNgramSize = new JSpinner();
		spinnerNgramSize.setModel(new SpinnerNumberModel(1, 1, 2, 1));
		// Spinner not editable
		JFormattedTextField tf = ((JSpinner.DefaultEditor) spinnerNgramSize
				.getEditor()).getTextField();
		tf.setEditable(false);
		spinnerNgramSize.setBounds(478, 44, 48, 20);
		add(spinnerNgramSize);

		JSeparator separator = new JSeparator();
		separator.setBounds(195, 79, 359, 1);
		add(separator);

		this.setPanelEnabled(false);

		textFilterDialog = new TextFilterDialog(GuiUtil.findParentFrame(this));
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnAddAlphabet.setActionCommand(ADD_ALPHABET);
		btnAddAlphabet.addActionListener(ctr);

		cbxAlphabet.setActionCommand(ALPHABET_SELECTED);
		cbxAlphabet.addActionListener(ctr);

		btnFilter.setActionCommand(FILTER_TEXT);
		btnFilter.addActionListener(ctr);

		btnAnalyze.setActionCommand(ANALYZE);
		btnAnalyze.addActionListener(ctr);

		textFilterDialog.addListener(ctr);
	}

	@Override
	public void setPanelEnabled(boolean b) {
		super.setPanelEnabled(b);
		if (b) {
			this.cbxAlphabet.setSelectedIndex(0);
			this.btnFilter.setEnabled(false);
			this.btnAnalyze.setEnabled(false);
		}
	}

	@Override
	public int getSelectedNgramSize() {
		Integer selectedValue = (Integer) spinnerNgramSize.getValue();
		return selectedValue;
	}

	@Override
	public int getSelectedAlphabetIndex() {
		return cbxAlphabet.getSelectedIndex();
	}

	@Override
	public void showAlphabets(List<String> alphabetNames) {
		alphabetNames.add(0, Messages.getString("guiSelectAlphabet"));
		GuiUtil.addItemsMaintainingSelected(cbxAlphabet, alphabetNames);
	}

	@Override
	public void enableFilter(boolean show) {
		btnFilter.setEnabled(show);
	}

	@Override
	public void enableAnalysis(boolean show) {
		btnAnalyze.setEnabled(show);
	}

	@Override
	public TextFilterView getTextFilterDialog() {
		return textFilterDialog;
	}
}
